package org.apache.commons.logging.impl;

import java.io.Serializable;
import java.util.logging.Level;

import org.apache.commons.logging.ILogLevel.LevelConstant;

import com.googlecode.gwt.commons.logging.impl.LogTimer;
import com.googlecode.gwt.commons.logging.jdk14log.Jdk14Logger;

public class Jdk14LoggerServer extends Jdk14Logger implements Serializable {

	private static LogTimer loggerServer = new LogTimer() {
		public LevelConstant getLevelConstant(int level) {
			LevelConstant temp = null;
			
			if(level == SEVERE.getIntValue()) {
				temp = SEVERE;
			}
			else if(level == WARNING.getIntValue()) {
				temp = WARNING;
			}
			else if(level == INFO.getIntValue()) {
				temp = INFO;
			}
			else if(level == ALL.getIntValue()) {
				temp = ALL;
			}
			else if(level == FINE.getIntValue()) {
				temp = FINE;
			}
			else if(level == FINEST.getIntValue()) {
				temp = FINEST;
			}
			else {
				temp = OFF;
			}
			
			return temp;
		}
	};
	
	private static final long serialVersionUID = -3497332440245830871L;

	/**
	 * Defne if send a list of client log to server, default false
	 */
	public static boolean sendToServer = false;
	
	public Jdk14LoggerServer() {
	}

	public Jdk14LoggerServer(String name) {
		super(name);
	}

	/**
	 * start to send a list of client log
	 *
	 */
	public void start() {
		loggerServer.start();
	}
	
	/**
	 * Stop to send a list of client log
	 *
	 */
	public void stop() {
		loggerServer.stop();	
	}
	
	protected boolean log(LevelConstant level, String msg, Throwable ex) {
		//boolean isLoggable = super.log(level, msg, ex); TODO
		boolean isLoggable = true;
		
		
		
		if (isLoggable && sendToServer) {
			String method = "";
			if(!isScript) {
				StackTraceElement locations = getStackTraceElement();
				method = "unknown";
				if (locations != null) {
					method = locations.getMethodName();
				}
			}
			loggerServer.addLog(level, getName(), method, msg, ex);	
		}
		
		return isLoggable;
	}
}
